#include "page3.h"

page3::page3(QWidget* parent)
	: QWidget(parent)
{
	this->hasReadFile = false;
	this->setWindowIcon(QIcon("Image\\运动会.svg"));
	QPixmap pixmap("Image\\光标.svg");
	//pixmap.scaled(40, 30, Qt::KeepAspectRatio, Qt::SmoothTransformation);
	pixmap = pixmap.scaled(30, 24, Qt::KeepAspectRatio);
	QCursor cursor(pixmap);
	this->setCursor(cursor);

	this->parent = dynamic_cast<SportMeeting*>(parent);
	ui.setupUi(this);
	ui.totaltable->horizontalHeader()->setDefaultAlignment(Qt::AlignHCenter);//表头字体居中
	ui.itemtable->horizontalHeader()->setDefaultAlignment(Qt::AlignHCenter);//表头字体居中
	connect(ui.pre_page, &QPushButton::clicked, this, &page3::pre_page);
	connect(ui.next_page, &QPushButton::clicked, this, &page3::next_page);
	connect(ui.teamBox,&QComboBox::currentIndexChanged, this, &page3::projectBox);
	connect(ui.admit, &QPushButton::clicked, this, &page3::admit);
	connect(ui.saveFile, &QPushButton::clicked, this, &page3::saveJsonFile);

}
page3::~page3()
{

}
void page3::setData(MyData* data) {
	this->myData = data;
}
void page3::setHasRead(bool hasRead) {
	this->hasReadFile = hasRead;
}
void page3::loadData() {
	// TODO
	int column = this->myData->getTeamNum();
	ui.totaltable->setColumnCount(column);
	ui.itemtable->setColumnCount(column);
	int row = this->myData->getItemNum1() + this->myData->getItemNum2();
	ui.totaltable->setRowCount(row);
	ui.itemtable->setRowCount(1);
	ui.teamBox->clear();
	
	QStringList labels;
	//vector<boyItem>& boyItems = this->myData->getBoyItems();
	//vector<girlItem>& girlItems = this->myData->getGirlItems();
	//vector<team>& teams = this->myData->getTeams();
	QVector<boyItem>& boyItems = this->myData->getBoyItems();
	QVector<girlItem>& girlItems = this->myData->getGirlItems();
	QVector<team>& teams = this->myData->getTeams();
	for (int i = 0; i < boyItems.size(); i++) {
		labels.append(boyItems[i].getName());
		ui.teamBox->addItem(boyItems[i].getName());
	}
	for (int i = 0; i < girlItems.size(); i++) {
		labels.append(girlItems[i].getName());
		ui.teamBox->addItem(girlItems[i].getName());
	}
	ui.totaltable->setVerticalHeaderLabels(labels);
	labels.clear();
	labels.append(ui.teamBox->currentText());
	ui.itemtable->setVerticalHeaderLabels(labels);
	labels.clear();
	for (int i = 0;i < teams.size();i++) {
		labels.append(teams[i].getName());
	}
	ui.totaltable->setHorizontalHeaderLabels(labels);
	ui.itemtable->setHorizontalHeaderLabels(labels);

	// 显示单元格内容
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < column; j++)
		{
			QTableWidgetItem* item = ui.totaltable->item(i, j);
			if (item == nullptr) {
				item = new QTableWidgetItem();
				ui.totaltable->setItem(i, j, item);
			}
			//else {
			//	for(int i=0;i<)
			//	item->setText("");
			//	//显示当前的队伍中的值

			//}
			ui.totaltable->item(i, j)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
		}
	}
	for (int j = 0; j < column; j++) {
		QTableWidgetItem* item = ui.itemtable->item(0, j);
		if (item == nullptr) {
			item = new QTableWidgetItem();
			ui.itemtable->setItem(0, j, item);
		}
		ui.itemtable->item(0, j)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
	}

	// readfile ok
	if (hasReadFile) {
		for (int j = 0; j < column; j++) {
			int* val = teams[j].getVal();
			for (int i = 0; i < row; i++) {
				QString text = QString::number(val[i]);
				QTableWidgetItem* item = ui.totaltable->item(i, j);
				item->setText(text);
			}
		}
	}

}


//槽函数
void page3::projectBox() {
	QStringList labels;
	labels.append(ui.teamBox->currentText());
	ui.itemtable->setVerticalHeaderLabels(labels);
	int column = this->myData->getTeamNum();
	for (int i = 0;i < column;i++) {
		QTableWidgetItem* item = ui.itemtable->item(0, i);
		if (item == nullptr) {
			item = new QTableWidgetItem();
			item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
			ui.itemtable->setItem(0, i, item);
		}
		else {
			item->setText("");
		}
	}

}
void page3::admit() {
	int column = this->myData->getTeamNum();
	int row = this->myData->getItemNum1() + this->myData->getItemNum2();
	vector<int> teamRank(column, 0);
	//判断是否对所有参赛队伍进行赋值
	QString valText;
	int val;
	bool isInt;
	int  accept = 1;
	for (int j = 0; j < column; j++) {
		valText = ui.itemtable->item(0, j)->text();
		val = valText.toInt(&isInt);
		if (!isInt) {
			fill(teamRank.begin(),teamRank.end(),0);
			message("警告", "未对所有参赛队伍进行排名", QMessageBox::Icon::Warning, QMessageBox::Ok, this);
			accept = 0;
			break;
		}
		teamRank[j] = val;
	}
	//判断对参赛队伍的排名是否正确
	if (accept == 1) {
		int tempJudge = 0;
		int total = ((column + 1) * column) / 2;
		for (int j = 0; j < column; j++) {
			if (teamRank[j] <= column && teamRank[j] >= 1) {
				tempJudge += teamRank[j];
			}
			
		}
		if (tempJudge==total) {
			QString currentName;
			//vector<boyItem>& boyItems = this->myData->getBoyItems();
			//vector<girlItem>& girlItems = this->myData->getGirlItems();
			//vector<team>& teams = this->myData->getTeams();
			QVector<boyItem>& boyItems = this->myData->getBoyItems();
			QVector<girlItem>& girlItems = this->myData->getGirlItems();
			QVector<team>& teams = this->myData->getTeams();
			currentName = ui.teamBox->currentText();
			for (int i = 0;i < boyItems.size();i++) {
				QString tempName = boyItems[i].getName();
				if (tempName == currentName) {
					int* itemVal = boyItems[i].getVal();
					for (int j = 0;j < column;j++) {
						boyItems[i].setRank(j,teamRank[j]);
						teams[j].setVal(i, itemVal[teamRank[j]-1]);
						QString str = QString::number(itemVal[teamRank[j]-1]);
						ui.totaltable->item(i, j)->setText(str);
					}
					break;
				}
			}
			for (int i = 0;i < girlItems.size();i++) {
				QString tempName = girlItems[i].getName();
				if (tempName == currentName) {
					int* itemVal = girlItems[i].getVal();
					for (int j = 0;j < column;j++) {
						girlItems[i].setRank(j, teamRank[j]);
						teams[j].setVal(i+boyItems.size(), itemVal[teamRank[j]-1]);
						QString str = QString::number(itemVal[teamRank[j]-1]);
						ui.totaltable->item(i+ boyItems.size(), j)->setText(str);
					}
					break;
				}
			}
		}
		else {
			message("警告", "参赛队伍排名有误", QMessageBox::Icon::Warning, QMessageBox::Ok, this);
		}
	}
	
}
void page3::pre_page() {
	setPage(parent, parent->pageTwo);
}
void page3::next_page() {
	int column = this->myData->getTeamNum();
	int row1 = this->myData->getItemNum1();
	int row2 = this->myData->getItemNum2();
	//vector<boyItem>& boyItems = this->myData->getBoyItems();
	//vector<girlItem>& girlItems = this->myData->getGirlItems();
	QVector<boyItem>& boyItems = this->myData->getBoyItems();
	QVector<girlItem>& girlItems = this->myData->getGirlItems();
	int accept = 1;
	for (int i = 0; i < row1 + row2; i++) {
		for (int j = 0; j < column; j++) {
			QString text = ui.totaltable->item(i, j)->text();
			bool isInt;
			int value = text.toInt(&isInt);
			if (!isInt) {
				accept = 0;
				message("警告", "还存在参赛队伍进行未排名", QMessageBox::Icon::Warning, QMessageBox::Ok, this);
				break;
			}
		}
		if (accept == 0) {
			break;
		}
	}
	if (accept == 1) {
		parent->pageFour->setData(this->myData);
		parent->pageFour->loadData();
		setPage(parent, parent->pageFour);
	}
}

void page3::saveJsonFile() {
	if (this->myData->writeDateToJson()) {
		
	}
	else {
		// 弹窗警告	message(QString title, QString text, QMessageBox::Icon, QMessageBox::Button, QWidget* parent);
		message("错误", "文件写入失败", QMessageBox::Icon::Critical, QMessageBox::Ok, this);
	}
}